package org.hot100_1;

import org.algorithm.List.ListNode;

public class leetcode28 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode dummy = new ListNode(-1);
        ListNode node = dummy;
        int carry =0;

        while (l1!=null || l2!=null){

            int num1 = l1==null ? 0 : l1.val;
            int num2 = l2==null ? 0 : l2.val;
            int sum = num1+num2+carry;
            carry=sum/10;
            sum = sum%10;
            node.next=new ListNode(sum);

            if (l1!=null){
                l1 = l1.next;
            }
            if (l2!=null){
                l2=l2.next;
            }

            node=node.next;
        }

        if (carry>0){
            node.next=new ListNode(carry);
        }

        return dummy.next;
    }
}
